import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class ResetCustomerPasswordFrame extends JFrame {

    public ResetCustomerPasswordFrame() {
        setTitle("重置客户密码");
        setSize(400, 300);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // 关闭时仅关闭当前窗口

        // 创建面板和布局
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(4, 2));
        add(panel);

        // 添加组件
        JLabel customerIdLabel = new JLabel("客户ID:");
        JTextField customerIdField = new JTextField();
        JLabel newPasswordLabel = new JLabel("新密码:");
        JPasswordField newPasswordField = new JPasswordField();
        JLabel confirmPasswordLabel = new JLabel("确认新密码:");
        JPasswordField confirmPasswordField = new JPasswordField();
        JButton resetButton = new JButton("重置密码");
        JButton cancelButton = new JButton("取消");

        panel.add(customerIdLabel);
        panel.add(customerIdField);
        panel.add(newPasswordLabel);
        panel.add(newPasswordField);
        panel.add(confirmPasswordLabel);
        panel.add(confirmPasswordField);
        panel.add(resetButton);
        panel.add(cancelButton);

        // 重置按钮事件监听器
        resetButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 获取输入的数据
                String customerId = customerIdField.getText();
                String newPassword = new String(newPasswordField.getPassword());
                String confirmPassword = new String(confirmPasswordField.getPassword());

                // 简单的密码验证逻辑
                if (newPassword.equals(confirmPassword)) {
                    // 在这里处理密码重置逻辑（例如：验证客户ID并更新新密码）
                    if (resetCustomerPassword(customerId, newPassword)) {
                        JOptionPane.showMessageDialog(null, "密码重置成功！");
                        dispose(); // 关闭当前窗口
                        new AdminMenuFrame().setVisible(true); // 返回管理员菜单界面
                    } else {
                        JOptionPane.showMessageDialog(null, "客户ID无效或重置失败！");
                    }
                } else {
                    JOptionPane.showMessageDialog(null, "新密码与确认密码不匹配！");
                }
            }
        });

        // 取消按钮事件监听器
        cancelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose(); // 关闭当前窗口
            }
        });
    }

    // 模拟密码重置的逻辑
    private boolean resetCustomerPassword(String customerId, String newPassword) {
        Connection connection = null;
        try {
            connection = DBUtil.getConnection(); // 获取数据库连接

            // SQL 查询，检查客户ID是否存在
            String checkCustomerSql = "SELECT COUNT(*) FROM customers WHERE id = ?";
            try (PreparedStatement checkStatement = connection.prepareStatement(checkCustomerSql)) {
                checkStatement.setString(1, customerId);
                ResultSet resultSet = checkStatement.executeQuery();
                if (resultSet.next()) {
                    int count = resultSet.getInt(1);
                    if (count == 0) {
                        return false; // 客户ID不存在
                    }
                }
            }

            // SQL 更新，更新客户密码
            String updatePasswordSql = "UPDATE customers SET password = ? WHERE id = ?";
            try (PreparedStatement updateStatement = connection.prepareStatement(updatePasswordSql)) {
                updateStatement.setString(1, newPassword);
                updateStatement.setString(2, customerId);
                int rowsAffected = updateStatement.executeUpdate();
                return rowsAffected > 0; // 如果更新了记录，则表示成功
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return false; // 如果发生错误，则返回失败
        } finally {
            DBUtil.closeConnection(connection); // 关闭数据库连接
        }
    }

}
